package cn.it.facultystaff.dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import cn.it.facultystaff.entity.FacultyStaff;

public class FacultyStaffDao {
	
	private  static String xmlDocumentPath;
	private static String path="D:\\FacultyStaff\\xmldocument\\facultystaff\\";
	
	
	
	public FacultyStaffDao() {
		xmlDocumentPath=createXmlDocumentName();
		createXmlFile();
	}

	private Document getXmlDocument(){
		
		//System.out.println(path);
		//获取xml文档
		SAXReader reader=new SAXReader();
		Document document = null;
		try {
			document = reader.read(path+File.separator+xmlDocumentPath);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return document;
	}
	
	public void addXmlElement(FacultyStaff facultyStaff){
		Document document=getXmlDocument();
		//获取根元素
		Element teachers=document.getRootElement();
	
		//添加子元素
		Element teacher=teachers.addElement("teacher");
		
		Element name=teacher.addElement("name");
		name.addText(facultyStaff.getName());
		
		Element jobNumber=teacher.addElement("jobNumber");
		jobNumber.addText(facultyStaff.getJobNumber());
		
		Element department=teacher.addElement("department");
		department.addText(facultyStaff.getDepartment());
		
		Element idNumber=teacher.addElement("idNumber");
		idNumber.addText(facultyStaff.getIdNumber());
		
		Element sex=teacher.addElement("sex");
		sex.addText(facultyStaff.getSex());
		
		Element photopath=teacher.addElement("photopath");
		photopath.addText(facultyStaff.getPhotopath());
		
		saveXmlToFile(document);
		
	}
	public FacultyStaff findXmlElement(String jobNumber){
		
		boolean isFind=false;
		File root = new File(path);
		SAXReader reader=new SAXReader();
		Document document = null;
		FacultyStaff facultyStaff=new FacultyStaff();
		
		for(File file : root.listFiles()) {
			String fileName=file.getName();
			
			try {
				document = reader.read(path+File.separator+fileName);
				
				Element teachers=document.getRootElement();
				Iterator iter=teachers.elementIterator();
				
				while(iter.hasNext()){
					Element teacher=(Element)iter.next();
					String jobNum=teacher.elementText("jobNumber");
					
					if(jobNum.equals(jobNumber)){
						facultyStaff.setJobNumber(teacher.elementText("jobNumber"));
						facultyStaff.setPhotopath(teacher.elementText("photopath"));
						isFind=true;
						break;
					}
				}
				
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(isFind){
				break;
			}
		}
		
		
		
		
		
		return facultyStaff;
	}
	
	public void updateXmlElement(FacultyStaff facultyStaff){
		
		boolean isFind=false;
		File root = new File(path);
		SAXReader reader=new SAXReader();
		Document document = null;
		
		for(File file : root.listFiles()) {
			String fileName=file.getName();
			try {
				document = reader.read(path+File.separator+fileName);
				
				Element teachers=document.getRootElement();
				Iterator iter=teachers.elementIterator();
				
				while(iter.hasNext()){
					Element teacher=(Element)iter.next();
					String jobNum=teacher.elementText("jobNumber");
					
					if(jobNum.equals(facultyStaff.getJobNumber())){
						 
						teacher.element("jobNumber").setText(facultyStaff.getJobNumber());
						teacher.element("photopath").setText(facultyStaff.getPhotopath());
						isFind=true;
						break;
					}
				}
				
				
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(isFind){
				break;
			}
		}
		
		saveXmlToFile(document);
	}
	private void saveXmlToFile(Document document) {
		//设置编码
		OutputFormat format=OutputFormat.createPrettyPrint();
		format.setEncoding("utf-8");
		//输出到文件
		XMLWriter writer;
		try {
			writer = new XMLWriter(new FileOutputStream(path+xmlDocumentPath),format);
			writer.write(document);
			writer.close();
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private  String createXmlDocumentName(){
		Date date=new Date();
		int year=date.getYear()+1900;
		int month=date.getMonth()+1;
		int day=date.getDate();
		
		return "facultystaff"+year+""+month+""+day+".xml";
	}
	
	private void createXmlFile(){
		File file = new File(path+File.separator+xmlDocumentPath);
		if(!file.exists()){
			Document document=DocumentHelper.createDocument();
			Element students=document.addElement("teachers");
			
			saveXmlToFile(document);
        }
	}

}
